package com.example.wiki.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wiki.aspect.LogAspect;
import com.example.wiki.entity.Ebook;
import com.example.wiki.entity.req.EbookQueryReq;
import com.example.wiki.entity.resp.CommonResp;
import com.example.wiki.entity.resp.EbookQueryResp;
import com.example.wiki.entity.resp.PageResp;
import com.example.wiki.exception.BusinessException;
import com.example.wiki.exception.BusinessExceptionCode;
import com.example.wiki.mapper.EbookMapper;
import com.example.wiki.service.IEbookService;
import com.example.wiki.util.CopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 电子书 服务实现类
 * </p>
 *
 * @author jerry
 * @since 2024-10-08
 */
@Service
public class EbookServiceImpl extends ServiceImpl<EbookMapper, Ebook> implements IEbookService {

    //日志对象
    private final static Logger LOG = LoggerFactory.getLogger(EbookServiceImpl.class);

    @Override
    public String uploadImage(MultipartFile file, String folder) {
        //判断文件是否为空
        if (file == null) {
            //抛出异常【运行时异常】
            throw new BusinessException(BusinessExceptionCode.Select_Upload_Image);
        }
        if (file.getSize() > 1024 * 1024 * 10) {//文件大于10mb
            throw new BusinessException(BusinessExceptionCode.Files_Larger_Than_10M);
        }
        //判断文件格式、大小
        //获取文件后缀【判断文件格式】
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length());
        if (!"jpg,jpeg,gif,png".toUpperCase().contains(suffix.toUpperCase())) {
            throw new BusinessException(BusinessExceptionCode.Files_Wrong_Format);
        }
        //生成一个唯一的文件名、上传文件
        String savePath = folder;
        File saveFile = new File(savePath);
        if (!saveFile.exists()) {
            //若不存在该目录，则创建目录
            saveFile.mkdir();
        }
        //通过UUID生成唯一文件名 文件名.jpg
        String filename = UUID.randomUUID().toString().replaceAll("-", "") + "." + suffix;
        try {
            //将文件保存指定目录 E:\实训\相思湖学院\软件3班\vuework\wiki\public\image\dsgdfgdfh.jpg  transferTo写出存放
            file.transferTo(new File(savePath + filename));
        } catch (Exception e) {
            e.printStackTrace();
            /*throw new BusinessException(BusinessExceptionCode.Save_File_Exception);*/
        }
        //返回文件名称
        LOG.info("文件名:{}", filename);
        return filename;
    }

    @Override
    public CommonResp<PageResp<EbookQueryResp>> query(EbookQueryReq req) {
        //定义响应对象
        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp<>();
        //条件构造器
        QueryWrapper<Ebook> wrapper = new QueryWrapper<>();
        //like
        wrapper.like(req.getName() != null && !req.getName().equals(""), "name", req.getName());
        wrapper.eq(req.getCategory2_id() != null && req.getCategory2_id() != 0, "category2_id", req.getCategory2_id());
        //构建page对象
        Page page = new Page(req.getPage(), req.getSize());
        //条件查询
        Page pageEbook = this.page(page, wrapper);
        //对象拷贝到EbookQueryResp
        List<EbookQueryResp> respList = CopyUtil.copyList(pageEbook.getRecords(), EbookQueryResp.class);
        //分页信息封装对象
        PageResp pageResp = new PageResp();
        pageResp.setTotal(pageEbook.getTotal());
        pageResp.setList(respList);
        //设置响应的数据、然后返回
        resp.setSuccess(true);
        resp.setContent(pageResp);
        return resp;
    }

}
